<?php
/**
 * 
 * System module
 * 
 * @name			System
 * @description		Standard system module for Cosa admin
 * @version			1.0.0
 * 
 * @author			Stijn Van Minnebruggen
 * @author_url		www.donotfold.be
 * @module_url		www.donotfold.be/projects/cosa
 * 
 * @sub1			system
 * @sub2			modules
 * @sub3			plugins
 * @sub4			domains
 * @sub5			languages
 * 
 */

class system extends Module
{
	
	/**
	 * Settings var
	 * 
	 */
	
	public $settings;
	
	
	/**
	 * Constructor function
	 * 
	 * @param	string	$var
	 * @return	bool
	 */
	
	public function __construct()
	{
		global $cosa, $db;
		$sql = "SELECT * FROM ".$db->table('settings')." WHERE type='system' ORDER BY `key`";
		$res = mysql_query($sql) or $cosa->oops('Couldn\'t fetch settings', 'db');
		while($row = mysql_fetch_array($res, MYSQL_ASSOC)) $this->settings[] = $row;
		return true;
	}
	
	
	/**
	 * Edit a user
	 * 
	 * @param	array	$postVars
	 * @param	int		$id
	 * @return	bool
	 */
	
	public function system_edit($postVars, $id)
	{
		// required
			global $cosa, $db, $lang, $modules;
		
		// edit settings
			foreach($this->settings as $k => $v) {
				$sql = "UPDATE ".$db->table('settings')." SET value = '".addslashes($postVars[$v['key']])."' WHERE `key` = '".addslashes($v['key'])."'";
				@mysql_query($sql) or $cosa->kill($lang->get('edit-system-failed'), 'db');
			}
		
		// return success
			$modules->addMessage($lang->get('edit-system-success'));
			return true;
		
	}
	
	
	/**
	 * Install a module
	 * 
	 * @param	array	$postVars
	 * @return	bool
	 */
	
	public function install($module)
	{
		// required
			global $modules, $lang;
		
		// install module
			$installed = $modules->install($module);
			if(!$installed) {
				$modules->addMessage($lang->get('add-modules-failed'));
				return false;
			}
		
		// return success
			$modules->addMessage($lang->get('add-modules-success'));
			return true;
		
	}
	
	
	/**
	 * Add a domain
	 * 
	 * @param	array	$postVars
	 * @return	bool
	 */
	
	public function domains_add($postVars)
	{
		// required
			global $cosa, $db, $user, $modules, $lang, $path;
			if(!isset($postVars) || empty($postVars)) {
				$modules->addMessage($lang->get('add-domains-failed'));
				return false;
			}
		
		// settings
			$active = '1';
			$default = '1';
		
		// add news
			$sql = "INSERT INTO ".$db->table('domains')."
					SET	name			= '".addslashes($postVars['name'])."'
						,active			= '".$active."'
						,`default`		= '".$default."'
					";
			@mysql_query($sql) or $cosa->kill($lang->get('add-domains-failed'), 'db');
			$news_id = mysql_insert_id();
		
		// return success
			$modules->addMessage($lang->get('add-domains-success'));
			return true;
		
	}
	
	
	/**
	 * Edit a domain
	 * 
	 * @param	array	$postVars
	 * @param	int		$id
	 * @return	bool
	 */
	
	public function domains_edit($postVars, $id)
	{
		// required
			global $cosa, $db, $user, $modules, $lang, $path;
			if(!isset($id) || empty($id)) {
				$modules->addMessage($lang->get('edit-domains-failed'));
				return false;
			}
		
		// settings
			$active = '1';
			$default = '1';
		
		// edit page
			$sql = "UPDATE ".$db->table('domains')."
					SET	name			= '".addslashes($postVars['name'])."'
						,active			= '".$active."'
						,`default`		= '".$default."'
					WHERE
						id				= '".addslashes($postVars['edit'])."'
					";
			@mysql_query($sql) or $cosa->kill($lang->get('edit-domains-failed'), 'db');
		
		// return success
			$modules->addMessage($lang->get('edit-domains-success'));
			return true;
		
	}
	
	
	/**
	 * Delete a domain
	 * 
	 * @param	int		$id
	 * @return	bool
	 */
	
	public function domains_del($id)
	{
		// required
			global $cosa, $db, $modules, $lang, $path;
			if(!isset($id) || empty($id)) {
				$modules->addMessage($lang->get('del-domains-failed'));
				return false;
			}
		
		// actual delete
			$sql = "DELETE FROM ".$db->table('domains')." WHERE id ='".addslashes($id)."' LIMIT 1";
			@mysql_query($sql) or $cosa->kill($lang->get('del-domains-failed'), 'db');
		
		// add message to pool
			$modules->addMessage($lang->get('del-domains-success'));
			return true;
		
	}
	
	
	/**
	 * Get one domain
	 * 
	 * @param	int		$id
	 * @return	array
	 */
	
	public function domains_get($id)
	{
		// required
			global $cosa, $db;
			if(!isset($id) || empty($id)) return false;
		
		// execute query
			$sql = "SELECT * FROM ".$db->table('domains')." WHERE id ='".addslashes($id)."' LIMIT 1";
			$res = @mysql_query($sql) or $cosa->oops('Could not fetch item.', 'db');
		
		// return result
			if(!mysql_num_rows($res)) return false;
			$row = mysql_fetch_array($res);
			return $row;
		
	}
	
	
	/**
	 * Add a language
	 * 
	 * @param	array	$postVars
	 * @return	bool
	 */
	
	public function languages_add($postVars)
	{
		// required
			global $cosa, $db, $user, $modules, $lang, $path;
			if(!isset($postVars) || empty($postVars)) {
				$modules->addMessage($lang->get('add-languages-failed'));
				return false;
			}
		
		// settings
			$active = '1';
			$default = '1';
		
		// add news
			$sql = "INSERT INTO ".$db->table('languages')."
					SET	domain_id			= '".addslashes($postVars['domain_id'])."'
						,name				= '".addslashes($postVars['name'])."'
						,short				= '".addslashes($postVars['short'])."'
						,email				= '".addslashes($postVars['email'])."'
						,text				= '".addslashes($postVars['text'])."'
						,active				= '".$active."'
						,`default`			= '".$default."'
						,domain_local_name	= '".addslashes($postVars['domain_local_name'])."'
					";
			@mysql_query($sql) or $cosa->kill($lang->get('add-languages-failed'), 'db');
			$news_id = mysql_insert_id();
		
		// return success
			$modules->addMessage($lang->get('add-languages-success'));
			return true;
		
	}
	
	
	/**
	 * Edit a language
	 * 
	 * @param	array	$postVars
	 * @param	int		$id
	 * @return	bool
	 */
	
	public function languages_edit($postVars, $id)
	{
		// required
			global $cosa, $db, $user, $modules, $lang, $path;
			if(!isset($id) || empty($id)) {
				$modules->addMessage($lang->get('edit-languages-failed'));
				return false;
			}
		
		// settings
			$active = '1';
			$default = '1';
		
		// edit page
			$sql = "UPDATE ".$db->table('languages')."
					SET	domain_id			= '".addslashes($postVars['domain_id'])."'
						,name				= '".addslashes($postVars['name'])."'
						,short				= '".addslashes($postVars['short'])."'
						,email				= '".addslashes($postVars['email'])."'
						,text				= '".addslashes($postVars['text'])."'
						,active				= '".$active."'
						,`default`			= '".$default."'
						,domain_local_name	= '".addslashes($postVars['domain_local_name'])."'
					WHERE
						id					= '".addslashes($postVars['edit'])."'
					";
			@mysql_query($sql) or $cosa->kill($lang->get('edit-languages-failed'), 'db');
		
		// return success
			$modules->addMessage($lang->get('edit-languages-success'));
			return true;
		
	}
	
	
	/**
	 * Delete a language
	 * 
	 * @param	int		$id
	 * @return	bool
	 */
	
	public function languages_del($id)
	{
		// required
			global $cosa, $db, $modules, $lang, $path;
			if(!isset($id) || empty($id)) {
				$modules->addMessage($lang->get('del-languages-failed'));
				return false;
			}
		
		// actual delete
			$sql = "DELETE FROM ".$db->table('languages')." WHERE id ='".addslashes($id)."' LIMIT 1";
			@mysql_query($sql) or $cosa->kill($lang->get('del-languages-failed'), 'db');
		
		// add message to pool
			$modules->addMessage($lang->get('del-languages-success'));
			return true;
		
	}
	
	
	/**
	 * Get one language
	 * 
	 * @param	int		$id
	 * @return	array
	 */
	
	public function languages_get($id)
	{
		// required
			global $cosa, $db;
			if(!isset($id) || empty($id)) return false;
		
		// execute query
			$sql = "SELECT * FROM ".$db->table('languages')." WHERE id ='".addslashes($id)."' LIMIT 1";
			$res = @mysql_query($sql) or $cosa->oops('Could not fetch item.', 'db');
		
		// return result
			if(!mysql_num_rows($res)) return false;
			$row = mysql_fetch_array($res);
			return $row;
		
	}
	
	
	/**
	 * Get all available domains
	 * 
	 * @return	array
	 */
	
	public function getDomains()
	{
		// required
			global $cosa, $db;
			$domains = array();
		
		// execute query
			$sql = "SELECT * FROM ".$db->table('domains')." ORDER BY name";
			$res = @mysql_query($sql) or $cosa->oops('Could not fetch domains.', 'db');
		
		// return result
			if(!mysql_num_rows($res)) return false;
			while($row = mysql_fetch_array($res)) $domains[] = $row;
			return $domains;
		
	}
	
	
}

?>